<?php

namespace app\api\controller\develop;
use app\api\service\develop\DatagridCustomService;
use app\api\service\log\AdminOperateLogService;
use app\BaseController;
use app\api\service\develop\DatagridToolbarService;
use app\util\AdminUserUtil;
use app\util\CommonUtil;
use app\api\service\develop\DatagridColumnService;
use app\util\ResponseUtil;
use app\util\UserUtil;
use think\App;

class DatagridToolbar extends BaseController
{
    private $datagridCode = 'datagrid_toolbar';
    private $arrFieldTitle;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $serviceDatagridColumn = new DatagridColumnService();
        $list = $serviceDatagridColumn->getListNoPage(array('datagrid_code' => $this->datagridCode, 'status' => 1));
        $this->arrFieldTitle = array_column($list, 'column_title', 'column_name');
    }

    public function getDatagridToolbarInfo()
    {
        $id = $this->requestData['id'];
        $service = new DatagridToolbarService();
        $finalData = $service->findRowById($id);
        return ResponseUtil::success($finalData);
    }

    public function getDatagridToolbarList()
    {
        $pageIndex = $this->requestData['page'];
        $pageSize = $this->requestData['rows'];
        $service = new DatagridToolbarService();
        $list = $service->getListWithPage($this->requestData, $pageIndex, $pageSize);
        $total = $service->getTotalNum($this->requestData);
        $finalData = CommonUtil::getPaginationData($list, $total);
        return ResponseUtil::success($finalData);
    }

    public function getDatagridToolbarOption()
    {
        $service = new DatagridToolbarService();
        $list = $service->getListNoPage($this->requestData);
        return ResponseUtil::success($list);
    }

    public function addDatagridToolbar()
    {
        try{
            if(empty($this->requestData['datagrid_code'])){
                return ResponseUtil::failure("页面标识码不可为空");
            }
            $datagridCode = $this->requestData['datagrid_code'];

            $serviceDatagridColumn = new DatagridColumnService();
            $columnList = $serviceDatagridColumn->getListNoPage(['datagrid_code' => $datagridCode, 'is_in_add'=>1, 'is_require' => 1]);
            foreach($columnList as $columnItem){
                if(empty($this->requestData[$columnItem['column_name']])){
                    return ResponseUtil::failure($columnItem['column_title']."不可为空");
                }
            }
            $service = new DatagridToolbarService();
            $res = $service->createRow($this->requestData);
            if($res){
                $addInfo = CommonUtil::getDataInsertMessage($this->requestData, $this->arrFieldTitle);
                $message = '新增页面工具栏';
                if($addInfo){
                    $message .= ','.$addInfo;
                }
                $logData = [
                    'subject_id' => $res,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('DatagridToolbar', AdminOperateLogService::ARR_ACTION_TYPE['add'], $logData);
                return ResponseUtil::success($res);
            }else{
                return ResponseUtil::failure("新增页面工具栏失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("新增页面工具栏失败");
        }
    }

    public function editDatagridToolbar()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的数据");
            }
            $datagridCode = $this->requestData['datagrid_code'];

            $serviceDatagridColumn = new DatagridColumnService();
            $columnList = $serviceDatagridColumn->getListNoPage(['datagrid_code' => $datagridCode, 'is_in_edit'=>1, 'is_require' => 1]);
            foreach($columnList as $columnItem){
                if(empty($this->requestData[$columnItem['column_name']])){
                    return ResponseUtil::failure($columnItem['column_title']."不可为空");
                }
            }

            $id = $this->requestData['id'];
            $service = new DatagridToolbarService();
            $oldInfo = $service->findRowById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("页面工具栏不存在");
            }
            $res = $service->updateRowById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData, $this->arrFieldTitle);
                $message = '编辑页面工具栏,'.$changeInfo['update_info'];
                $logData = [
                    'subject_id' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('DatagridToolbar', AdminOperateLogService::ARR_ACTION_TYPE['add'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑页面工具栏失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑页面工具栏失败");
        }
    }

    public function deleteDatagridToolbar()
    {
        try{
            $id = $this->requestData['id'];
            $service = new DatagridToolbarService();
            $info = $service->findRowById($id);
            if(!$info){
                return ResponseUtil::failure("数据不存在");
            }
            $res = $service->removeRowById($id);
            if($res){
                $deleteInfo = CommonUtil::getDataDeleteMessage($info, $this->arrFieldTitle);
                $message = '删除页面工具栏';
                if($deleteInfo){
                    $message .= ','.$deleteInfo;
                }
                $logData = [
                    'subject_id' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('DatagridToolbar', AdminOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除页面工具栏失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除页面工具栏失败");
        }

    }

    public function deleteMultiDatagridToolbar(){
        try{
            $arrId = $this->requestData['arr_id'];
            if(!is_array($arrId)){
                return ResponseUtil::failure("参数格式有误");
            }
            $service = new DatagridToolbarService();
            $deleteData = [];
            foreach($arrId as $value){
                $info = $service->findRowById($value);
                if(!$info){
                    return ResponseUtil::failure("数据不存在");
                }
                $deleteData[] = $info;
            }
            $res = $service->removeListByManyId($arrId);
            if($res){
                $deleteInfo = CommonUtil::getDataDeleteMessage($deleteData, $this->arrFieldTitle, false, array('button_text'));
                $message = '删除页面工具栏,'.$deleteInfo;
                $logData = [
                    'subject_id' => $arrId[0],
                    'remark'  => json_encode($arrId),
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('DatagridToolbar', AdminOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除页面工具栏失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除页面工具栏失败");
        }
    }

    public function toggleDatagridToolbar()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的数据");
            }

            $arrField = ['status'];
            $arrFieldTitle = ['status'=>'状态'];
            if(empty($this->requestData['field_name'])){
                return ResponseUtil::failure("请指定要编辑的页面工具栏");
            }

            $id = $this->requestData['id'];
            $fieldName = $this->requestData['field_name'];
            if(!in_array($fieldName, $arrField)){
                return ResponseUtil::failure("不支持编辑当前页面工具栏");
            }

            $service = new DatagridToolbarService();
            $oldInfo = $service->findRowById($id);

            $updateData[$fieldName] = empty($this->requestData['value']) ? 2:$this->requestData['value'];
            $res = $service->updateRowById($id, $updateData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $updateData,$arrFieldTitle);
                $message = '编辑页面工具栏,'.$changeInfo['update_info'];
                $logData = [
                    'subject_id' => $id,
                    'remark'  => $oldInfo['button_text'],
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('DatagridToolbar', AdminOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("更新页面工具栏失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("更新页面工具栏失败");
        }
    }

    public function simpleUpdateDatagridToolbar()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的数据");
            }

            $id = $this->requestData['id'];
            $service = new DatagridToolbarService();
            $oldInfo = $service->findRowById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("数据不存在");
            }
            $res = $service->updateRowById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData, $this->arrFieldTitle);
                $message = '编辑页面工具栏,'.$changeInfo['update_info'];
                $logData = [
                    'subject_id' => $id,
                    'remark'  => $oldInfo['button_text'],
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('DatagridToolbar', AdminOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑页面工具栏失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑页面工具栏失败");
        }
    }


}



